package com.xpu.cos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xpu.cos.entity.Orders;
import com.xpu.cos.mapper.OrderMapper;
import com.xpu.cos.result.Result;
import com.xpu.cos.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xpu.cos.utils.TokenUtils;
import org.apache.el.parser.Token;
import org.mockito.internal.matchers.Or;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ambitionjh
 * @since 2024-01-02
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements OrderService {

    @Override
    public Result addOrder(Orders order) {
        order.setUserId(Integer.parseInt(TokenUtils.getUserId()));
        boolean saved = save(order);
        return saved? Result.ok():Result.fail();
    }

    @Override
    public Result deleteOrder(Integer orderId) {
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getId,orderId);
        boolean removed = remove(wrapper);
        return removed? Result.ok():Result.fail();
    }

    @Override
    public Result getAllOrder(Integer userId) {
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getUserId,userId);
        List<Orders> list = list(wrapper);
        return Result.ok(list);
    }
}
